﻿using Learun.Application.TwoDevelopment.ZZDT_EC;
using Learun.Util;
using Learun.Util.SqlSugar;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;

namespace Learun.Application.Web.Areas.ZZDT_EC.Controllers
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-01-30 15:57
    /// 描 述：图纸文件
    /// </summary>
    public class ec_drawing_fileController : MvcControllerBase
    {
        private ec_drawing_fileIBLL ec_drawing_fileIBLL = new ec_drawing_fileBLL();

        #region 视图功能

        /// <summary>
        /// 主页面
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 表单页
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Form()
        {
            return View();
        }
        /// <summary>
        /// 选择页
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult SelectIndex()
        {
            return View();
        }
        /// <summary>
        /// 回收站
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult RecycleBin()
        {
            return View();
        }
        #endregion

        #region 获取数据

        /// <summary>
        /// 获取列表数据
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetList(string queryJson)
        {
            var data = ec_drawing_fileIBLL.GetList(queryJson);
            return Success(data);
        }
        /// <summary>
        /// 获取列表分页数据
        /// <param name="pagination">分页参数</param>
        /// <param name="engineDataID">工程数据ID</param>
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetPageList(string pagination, string queryJson, string engineDataID = "")
        {
            Pagination paginationobj = pagination.ToObject<Pagination>();
            var data = ec_drawing_fileIBLL.GetList(queryJson, engineDataID, paginationobj);
            var jsonData = new
            {
                rows = data,
                total = paginationobj.total,
                page = paginationobj.page,
                records = paginationobj.records
            };
            return Success(jsonData);
        }
        /// <summary>
        /// 获取表单数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetFormData(string keyValue, string ProjectId)
        {
            var data = ec_drawing_fileIBLL.GetEntity(keyValue, ProjectId);
            var jsonData = new
            {
                ec_drawing_file = data
            };
            return Success(jsonData);
        }
        /// <summary>
        /// 获取图纸目录文件树形数据。
        /// 场合：比如电缆清册输出时的选择。
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetTreeData(string ProjectId)
        {
            var tableName = ProjectSugar.TableName<ec_drawing_fileEntity>(ProjectId);
            var catalogueTableName = ProjectSugar.TableName<ec_drawing_catalogueEntity>(ProjectId);

            var list = SqlSugarHelper.Db.Queryable<ec_drawing_catalogueEntity>().AS(catalogueTableName).Where(x=>x.DrawingCatalogueName != "材料表").ToList();

            var ids = list.Select(x => x.DrawingCatalogueID).ToList();
            var files = SqlSugarHelper.Db.Queryable<ec_drawing_fileEntity>().AS(tableName)
                .Where(x => ids.Contains(x.DrawingCatalogueID)).ToList();
            files = files.Where(x => x.IsDelete != 1).ToList();
            List<TreeModel> treeList = new List<TreeModel>();
            foreach (var item in list)
            {
                TreeModel node = new TreeModel();
                node.id = item.DrawingCatalogueID;
                node.text = item.DrawingCatalogueName;
                node.value = item.DrawingCatalogueID;
                node.showcheck = false;
                node.checkstate = 0;
                node.isexpand = false;
                node.parentId = item.UpDrawingCatalogueID;
                node.NodeExtData = item;
                treeList.Add(node);
            }
            foreach (var item in files)
            {
                TreeModel node = new TreeModel();
                node.id = item.DrawingFileID;
                node.text = item.DrawingFileName;
                node.value = item.DrawingFileID;
                node.showcheck = false;
                node.checkstate = 0;
                node.isexpand = false;
                node.parentId = item.DrawingCatalogueID;
                node.NodeExtData = item;
                treeList.Add(node);
            }
            return Success(treeList.ToTree());
        }
        #endregion

        #region 提交数据

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult DeleteForm(string keyValue, string ProjectId)
        {
            ec_drawing_fileIBLL.DeleteEntity(keyValue, ProjectId);
            return Success("删除成功！");
        }
        /// <summary>
        /// 逻辑删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult LogicDelete(string keyValue, string ProjectId)
        {
            ec_drawing_fileIBLL.LogicDelete(keyValue, ProjectId);
            return Success("删除成功！");
        }

        /// <summary>
        /// 还原逻辑删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult RepeatLogicDelete(string keyValue, string ProjectId)
        {
            ec_drawing_fileIBLL.RepeatLogicDelete(keyValue, ProjectId);
            return Success("还原成功！");
        }

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult SaveForm(string keyValue, string strEntity, string ProjectId)
        {
            ec_drawing_fileEntity entity = strEntity.ToObject<ec_drawing_fileEntity>();
            //同一目录下不允许有名称相同的图纸文件
            var data = ec_drawing_fileIBLL.GetList("{DrawingCatalogueID:\"" + entity.DrawingCatalogueID + "\",ProjectId:\"" + ProjectId + "\",DrawingFileNameFull:\"" + entity.DrawingFileName + "\"}").ToList();
            if (data != null && data.Count > 0)
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    if (data.Find(x => x.DrawingFileID != keyValue) != null)
                    {
                        return Fail("同一目录下不允许有名称相同的图纸文件！");
                    }
                }
                else
                {
                    return Fail("同一目录下不允许有名称相同的图纸文件！");
                }
            }
            if (!string.IsNullOrEmpty(keyValue))
            {
                ec_drawing_fileEntity entityOld = ec_drawing_fileIBLL.GetEntity(keyValue, ProjectId);
                entity.IsPublish = entityOld.IsPublish;
                entity.IsCheckOut = entityOld.IsCheckOut;
                entity.CheckOutUserID = entityOld.CheckOutUserID;
                entity.CheckOutTime = entityOld.CheckOutTime;
            }
            ec_drawing_fileIBLL.SaveEntity(keyValue, entity, ProjectId);
            return Success("保存成功！");
        }

        /// <summary>
        /// 释放图纸文件
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult FreeDrawingFile(string keyValue, string ProjectId)
        {
            ec_drawing_fileIBLL.FreeDrawingFile(keyValue, ProjectId);
            return Success("释放成功！");
        }

        /// <summary>
        /// 图纸导入  类似于复制的功能
        /// <param name="DrawingCatalogueID">图纸目录ID</param>
        /// <param name="strEntity">包含DrawingFileID和ProjectId</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult SaveSelectData(string drawingCatalogueID, string ProjectId, string strEntity)
        {
            string msg = ec_drawing_fileIBLL.SaveSelectData(drawingCatalogueID, ProjectId, strEntity);
            if (string.IsNullOrEmpty(msg))
            {
                return Success("导入成功！");
            }
            else
            {
                return Fail(msg);
            }
        }

        #endregion

    }
}
